public class ReverseKGroup {
    // leetcode k个一组翻转链表
    // https://leetcode.cn/problems/reverse-nodes-in-k-group/description/?envType=study-plan-v2&envId=top-100-liked
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode cur = head, prev = null;
        for (int i = 0; i < k; i++) {
            if (cur == null) return head;
            cur = cur.next;
        }
        ListNode next = reverseKGroup(cur,k);
        prev = next;
        ListNode cur1 = head;
        while (cur1 != cur) {
            ListNode nextCur = cur1.next;
            cur1.next = prev;
            prev = cur1;
            cur1 = nextCur;
        }
        return prev;
    }
}
